package com.ad.pet.app.photo.loader.ui;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.ad.pet.R;
import com.ad.pet.app.photo.loader.DaggerPhotoLoaderComponent;
import com.ad.pet.app.photo.loader.PhotoLoaderComponent;
import com.ad.pet.app.photo.loader.PhotoLoaderContract;
import com.ad.pet.app.photo.loader.PhotoLoaderPresenter;
import com.ad.pet.app.photo.model.BucketImageZipModel;
import com.ad.pet.common.data.injector.HasComponent;
import com.ad.pet.common.data.utils.ui.ResourcesUtil;
import com.ad.pet.common.structure.mvp.BasePresenter;
import com.ad.pet.common.structure.ui.activity.AppToolbarActivity;

import javax.inject.Inject;


/**
 * 加载相册的activity
 */

public class PhotoLoaderActivity extends AppToolbarActivity implements PhotoLoaderContract.View
        , HasComponent<PhotoLoaderComponent> {
    public static final int REQUEST_PHOTO_CODE = 0x301;
    @Inject
    PhotoLoaderPresenter mPresenter;
    @Inject
    ResourcesUtil mResourcesUtil;
    private PhotoLoaderComponent mComponent;
    public BucketFragment mBucketFragment;
    //可选择的最大数量
    private int mSelectedMax;
    private ImageFragment mImageFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setToolbarTitle(getString(R.string.bucket));
        //重写返回按钮  使用回退栈
        mToolbar.setBackOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        if (mSelectedMax > 0) {
            setToolbarNext(getString(R.string.cancel), new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mImageFragment.resetSelectData();
//                    ArrayList<ImageItem> selectedDatas = mBucketFragment.getImageFragment().getSelectedData();
//                    ArrayList<String> urls = new ArrayList<>();
//                    for (int i = 0; i < selectedDatas.size(); i++) {
//                        ImageItem imageItem = selectedDatas.get(i);
//                        urls.add(imageItem.getImagePath());
//                    }
//
//                    Intent intent = getIntent();
//                    intent.putStringArrayListExtra("path", urls);
//                    setResult(RESULT_OK, intent);
                    finish();


                }
            });
        }
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {
        mStatusLayoutManager.showError(msg);
    }

    @Override
    public void loaderPhotos(BucketImageZipModel buckets) {
        if (buckets != null && !buckets.getBucketImage().isEmpty()) {
            mStatusLayoutManager.showContent();
            //文件夹
            mBucketFragment = BucketFragment.newInstance(buckets.getBucketImage());

            getSupportFragmentManager().beginTransaction().add(R.id.fl_container, mBucketFragment)
                    .show(mBucketFragment)
                    .commit();

            //所有图片
            mImageFragment = ImageFragment.newInstance(buckets.getImageItem());
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fl_container, mImageFragment)
                    .show(mImageFragment)
                    .hide(mBucketFragment)
                    .addToBackStack(getClass().getSimpleName())
                    .commit();
            return;
        }

        mStatusLayoutManager.showEmpty();
    }

    @Override
    protected int contentLayout() {
        return R.layout.activity_photo_loader;
    }

    @Override
    protected void clickRetry(View v) {
        mPresenter.getPhotos();
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();

        mSelectedMax = intent.getIntExtra("selectedMax", 0);


        mPresenter.onCreate(this);
        mPresenter.getPhotos();
    }

    @Override
    protected void initEvents() {

    }

    @Override
    protected void initViews() {

    }

    @Override
    public void initInject() {
        mComponent = DaggerPhotoLoaderComponent.builder().applicationComponent(getApplicationComponent())
                .activityModule(getActivityModule())
                .build();
        mComponent.inject(this);
    }

    protected BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public PhotoLoaderComponent getComponent() {
        return mComponent;
    }


    public int getSelectedMax() {
        return mSelectedMax;
    }
}
